package org.linlinjava.litemall.core.storage;

import com.sina.cloudstorage.auth.AWSCredentials;
import com.sina.cloudstorage.auth.BasicAWSCredentials;
import com.sina.cloudstorage.services.scs.SCS;
import com.sina.cloudstorage.services.scs.SCSClient;
import com.sina.cloudstorage.services.scs.model.ObjectMetadata;
import com.sina.cloudstorage.services.scs.model.PutObjectResult;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Description
 * @Author fanlei
 * @Version 1.0
 * @Date 2018年08月05日
 */
public class SinaStorage implements Storage{

    private  String endpoint;
    private  String accessKeyId;
    private  String accessKeySecret;
    private  String bucketName;
    private  String path;

    /**
     * @Description
     * @Param 获取新浪OSS客户端对象
     * @Return com.sina.cloudstorage.services.scs.SCS
     */
    private SCS getOSSClient() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, accessKeySecret);
        return new SCSClient(credentials);
    }

    private String getBaseUrl(String keyName) {
        if(path != null){
            keyName = path + "/" + keyName;
        }
        return "http://" + bucketName + "." +  endpoint + "/" +keyName;
    }

    /**
     *
     * http://scs.sinacloud.com/doc/scs/sdk/sdk/java参考
     *
     * @Description
     * @Param [file, keyName]
     * @Return void
     */
    @Override
    public void store(MultipartFile file, String keyName) {
        if(path != null){
            keyName = path + "/" + keyName;
        }
        ObjectMetadata data = new ObjectMetadata();
        data.setContentType(file.getContentType());
        data.setContentLength(file.getSize());
        //自定义请求头k-v
        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("x-amz-acl", "public-read");
        data.setUserHeader(requestHeader);
        try {
            PutObjectResult putObjectResult = getOSSClient().putObject(bucketName, keyName,
                    file.getInputStream(), data);
            System.out.println(putObjectResult);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Stream<Path> loadAll() {
        return null;
    }

    @Override
    public Path load(String keyName) {
        return null;
    }

    @Override
    public Resource loadAsResource(String keyName) {
        return null;
    }

    @Override
    public void delete(String keyName) {
        if(path != null){
            keyName = path + "/" + keyName;
        }
        getOSSClient().deleteObject(bucketName, keyName);
    }

    @Override
    public String generateUrl(String keyName) {
        return getBaseUrl(keyName);
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
